<template>
  <div class="article">
    <van-nav-bar title="文章详情" left-arrow @click-left="$router.back()" />
    <div class="wrap_con">
      <h3>{{ info.title }}</h3>
      <div class="myconfirm">
        <div>
          <van-image
            round
            width="60px"
            height="60px"
            fit="cover"
            :src="info.aut_photo"
          />
        </div>
        <div class="name">
          <div>{{ info.aut_name }}</div>
          <div>{{ info.pubdate | relvTime }}</div>
        </div>
        <div v-if="$store.state.userInfo.id != info.aut_id">
          <van-button
            @click="doFollow(true)"
            v-if="!info.is_followed"
            round
            type="info"
            >+关注</van-button
          >
          <van-button @click="doFollow(false)" v-else round type="info"
            >-取关</van-button
          >
        </div>
      </div>
      <div class="content" v-html="info.content">我是内容</div>
      <van-divider>end</van-divider>
      <div class="btn-wrap">
        <van-button v-if="info.attitude==-1" @click="zan(true)" plain type="primary"  round icon="good-job-o"
          >点赞</van-button
        >
        <van-button v-if="info.attitude==1" @click="zan(false)" plain type="primary"  round icon="good-job"
          >取消点赞</van-button
        >
        <div v-if="info.attitude == -1" style="width: 30px"></div>
        <van-button @click="unlike(true)" v-if="info.attitude == -1" plain type="danger" round icon="delete">不喜欢 </van-button>
        <van-button @click="unlike(false)" v-if="info.attitude == 0" plain type="danger" round icon="delete">取消不喜欢 </van-button>
      </div>
    </div>
    <components :id="$route.query.id"></components>
  </div>
</template>

<script>
import { articleInfoAPI, followUserAPI, unfollowUserAPI,zanAPI,unzanAPI,unlikeAPI,cancelUnlikeAPI } from "@/api";
import Components from "./components.vue";
export default {
  components: { Components },
  data() {
    return {
      info: {},
    };
  },
  async created() {
    let res = await articleInfoAPI(this.$route.query.id);
    console.log(res);
    this.info = res.data.data;
  },
  methods: {
    async doFollow(status) {
      this.info.is_followed = status;
      try {
        if (status) {
          await followUserAPI({
            target: this.info.aut_id,
          });
        } else {
          await unfollowUserAPI(this.info.aut_id);
        }
        this.$toast.success("操作成功");
      } catch {
        this.$toast.fail("操作失败！请与管理员联系");
      }
    },
    async zan(status){
      if(status){
        await zanAPI({
          target:this.info.art_id
        });
        this.info.attitude=1
      }else{
        await unzanAPI(this.info.art_id)
        this.info.attitude=-1
      }
    },
    async unlike(status){
      if(status){
        await unlikeAPI({
          target:this.info.art_id
        })
        this.info.attitude=0
      }else{
        await cancelUnlikeAPI(this.info.art_id);
        this.info.attitude=-1
      }
    }
  },
};
</script>

<style lang="less" scoped>
.article {
  .wrap_con {
    margin: 20px 20px;
    h3 {
      font-size: 25px;
    }
    .myconfirm {
      display: flex;
      height: 80px;
      align-items: center;
      .name {
        margin-left: 15px;
        flex: 1;
      }
    }
    .btn-wrap {
      display: flex;
      justify-content: center;
      .van-button {
        width: 120px;
      }
    }
    .content {
      word-break: break-word;
      overflow: hidden;
      ::v-deep {
        // 这个标签是一个行内元素，所以设置宽度没用
        pre {
          code {
            display: block;
            width: 100%;
            overflow: auto;
            background-color: #101010;
            color: #dcdcdc;
          }
        }

        img {
          width: 80%;
        }
      }
    }
  }
}
</style>